﻿
var selectdate = '';
var yaodian;

$(function () {
    var user = getCookie('user');
    var org = getCookie('org');
    yaodian = getCookie('orgid');
    if (user == undefined || user == null || user == '') {
        //alert("请重新登陆！");
        $.alert("请重新登陆！", "未登录", function () {
            //return alert('回调');
            window.location.href = '../login.html';
        });
    } else {
        //alert('当前用户：' + user);
        $('#dqyd').text('当前门店：' + org);
    }

    var $input_dhhm = $('#js_input_dhhm');
    $input_dhhm.on('input', function () {
        if ($input_dhhm.val().length > 11) {
            $input_dhhm.val($input_dhhm.val().slice(0, 11));
        }
    });

    $("#birthday").datetimePicker({
        title: '日期',
        years: range(2020, 2022),
        times: function () { return []; },
        parse: function (str) {
            return str.split("-");
        },
        onChange: function (picker, values, displayValues) {
            selectdate = values[0] + values[1] + values[2];
            //console.log(values);
        }
    });

    dangqianyaodiankucun();
});

//获取当前药店库存
function dangqianyaodiankucun() {
    $.ajax({
        type: "GET",
        url: "http://" + A_Url + "/api/yudingfind?info=getdangqianyaodiankucun&orgid=" + yaodian,
        //数据，json字符串
        //data: JSON.stringify(list),
        success: function (result) {
            $('#dqyd_kc').text('线上库存:' + (result.WangLuoShu - result.WangLuoYuDingShu) + '   线下库存:' + (result.XianXiaShu - result.XianXiaMaiChuShu));
        },
        error: function (e) { }
    });

}


//获取已经预定信息
function GetOrder() {
    var ShenFenZheng = $('#js_input_sfz').val();
    var telephone = $('#js_input_dhhm').val();



    if (telephone == '' && ShenFenZheng == '') {
        $.alert("请输入身份证或电话！", "信息不全", function () {
        });
        return;
    }


    if (ShenFenZheng != '') {
        if (!checkIdCard(ShenFenZheng)) {
            $.alert("身份证校验失败！", "信息不全", function () {
            });
            return;
        }
    }

    if (telephone != '') {
        if (!checkPhone(telephone)) {
            $.alert("手机号证校验失败！", "信息不全", function () {
            });
            return;
        }
    }


    $.showLoading();
    var tm = setTimeout(function () {
        $.hideLoading();
        $.alert("系统繁忙，请稍后再试！", "超时", function () { });
    }, 20000);

    $.ajax({
        type: "GET",
        url: "http://" + A_Url + "/api/yudingfind",
        //数据，json字符串
        data: { info: 'getorderbyparam', ShenFenZheng: ShenFenZheng, telephone: telephone, selectdate: selectdate, orgid: yaodian },
        success: function (result) {
            $.hideLoading();
            clearTimeout(tm);
            if (result == undefined || result == null) {
                $.alert("没有预定记录！", "未预定", function () {
                    return;
                });
            } else {
                if (result.length > 0) {
                    var tbstr = '';
                    for (var a = 0; a < result.length; a++) {

                        if (result[a].ZhuangTai == 0) {
                            if (result[a].YuDingShu != result[a].XiaoShuLiang && result[a].YuDingShu > result[a].XiaoShuLiang) {
                                tbstr += "<tr onclick=\"openrecord('" + result[a].YuDingRiQi + "','" + result[a].ID + "');\"><td title='姓名:'>" + result[a].XingMing
                                    + "</td><td title='身份证:'>" + result[a].ShenFenZheng;
                                tbstr += "</td><td title='线上订单：' class='f-green'>" + '请点击处理' + "</td></tr>";
                            } else {
                                tbstr += "<tr><td title='姓名:'>" + result[a].XingMing
                                    + "</td><td title='身份证:'>" + result[a].ShenFenZheng;
                                tbstr += "</td><td title='线上订单：' class='f-red'>" + '订单已完成，无需点击' + "</td></tr>";
                            }
                        } else if (result[a].ZhuangTai == 1) {
                            tbstr += "<tr><td title='姓名:'>" + result[a].XingMing
                                + "</td><td title='身份证:'>" + result[a].ShenFenZheng
                                + "</td><td title='线下订单：' class='f-red'>" + '无需处理' + "</td></tr>";
                        }

                    }
                    $('#table_list01').html(tbstr);
                } else {
                    $('#table_list01').html('');
                    //代客下单
                    $.confirm("是否代客下单？", "没有记录", function () {
                        toorder(ShenFenZheng, telephone);
                    }, function () {
                        //取消操作
                    });
                }
            }
        },
        error: function (e) { }
    });

}

function openrecord(selectdateint, orderid) {
    if (selectdateint != undefined && orderid != undefined)
        window.location.href = "http://" + B_Url + '/Main/weixin/yuding_Customer_order.html?id=' + orderid + '&selectdate=' + selectdateint;
}

function toorder(ShenFenZheng, telephone) {
    window.location.href = "http://" + B_Url + '/Main/weixin/yuding_Replace_order.html?ShenFenZheng=' + ShenFenZheng + '&telephone=' + telephone;
}





